package com.uphone.edwards;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.radish.baselibrary.Intent.IntentUtils;
import com.uphone.edwards.adapter.ContentsAdapter;
import com.uphone.edwards.adapter.LanguageAdapter;
import com.uphone.edwards.adapter.MainJikanAdapter;
import com.uphone.edwards.base.mvp.BaseMvpActivity;
import com.uphone.edwards.pro.activity.login.HandbookContentActivity;
import com.uphone.edwards.pro.activity.login.contancts.ContanctsActivity;
import com.uphone.edwards.pro.activity.login.mine.MineActivity;
import com.uphone.edwards.util.LanguageType;
import com.uphone.edwards.util.LanguageUtils;
import com.uphone.edwards.util.SharedUtils;
import com.uphone.edwards.view.NoScrollViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseMvpActivity<MainPresenter> implements MainContract.View {
    @BindView(R.id.ll_tomine)
    LinearLayout llTomine;
    @BindView(R.id.lvMain)
    ListView lvMain;
    @BindView(R.id.tv_to_read)
    TextView tvToRead;
    @BindView(R.id.lv_lang)
    ListView lvLang;
    @BindView(R.id.tv_shouce)
    TextView tvShouce;
    @BindView(R.id.tv_jikan)
    TextView tvJikan;
    @BindView(R.id.vp_main)
    NoScrollViewPager vpMain;
    @BindView(R.id.bottom_navigation)
    BottomNavigationView bottomNavigation;
    @BindView(R.id.ll_shouce)
    LinearLayout llShouce;
    @BindView(R.id.ll_jikan)
    LinearLayout llJikan;

    ContentsAdapter cAdapter;
    LanguageAdapter langAdapter;
    @BindView(R.id.ll_language)
    LinearLayout llLanguage;
    @BindView(R.id.lv_contancts)
    ListView lvContancts;
    @BindView(R.id.dl_main)
    DrawerLayout dlMain;
    @BindView(R.id.ll_drawer)
    LinearLayout llDrawer;
    @BindView(R.id.tv_mulu)
    TextView tvMulu;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_main_one)
    RecyclerView rvMainOne;
    @BindView(R.id.rv_main_two)
    RecyclerView rvMainTwo;
    @BindView(R.id.tv_main_language)
    TextView tvMainLanguage;
    //true 代表隐藏状态，false代表显示状态    语言选择弹框
    private boolean langFlag;

    private MainJikanAdapter mainJikanAdapter;

    private MainPresenter mainPresenter;

    @Override
    protected void initBundle() {

    }

    @Override
    protected int initLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mainPresenter = new MainPresenter();
        mainPresenter.attachView(this);

        mainPresenter.postBanner();
        mainPresenter.postMulu();


        cAdapter = new ContentsAdapter(this);
        langAdapter = new LanguageAdapter(this);
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("a");
        list.add("a");
        list.add("a");
        list.add("a");
        list.add("a");
        list.add("a");
        list.add("a");
        list.add("a");
        list.add("other");
        lvMain.setAdapter(cAdapter);

        List<Integer> listInt = new ArrayList<>();
        listInt.add(R.string.zhongwen);
        listInt.add(R.string.yingyu);
        listInt.add(R.string.riwen);
        listInt.add(R.string.hanwen);
        lvLang.setAdapter(langAdapter);
        langAdapter.setData(listInt);


        //判断设置对应语言
        if (LanguageType.ENGLISH.getLanguage().equals(SharedUtils.getShareLanguage(this))){
            tvMainLanguage.setText(getResources().getString(R.string.yingyu));
        }
        if (LanguageType.CHINESE.getLanguage().equals(SharedUtils.getShareLanguage(this))){
            tvMainLanguage.setText(getResources().getString(R.string.zhongwen));
        }
        if (LanguageType.KOREA.getLanguage().equals(SharedUtils.getShareLanguage(this))){
            tvMainLanguage.setText(getResources().getString(R.string.hanwen));
        }
        if (LanguageType.JAPAN.getLanguage().equals(SharedUtils.getShareLanguage(this))){
            tvMainLanguage.setText(getResources().getString(R.string.riwen));
        }



        //设置侧滑
        dlMain.setScrimColor(getResources().getColor(R.color.main_scrim_color));
        lvContancts.setAdapter(cAdapter);
        cAdapter.setData(list);

        //指定列表线性布局，横向水平
        LinearLayoutManager lm = new LinearLayoutManager(this);
        lm.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvMainOne.setLayoutManager(lm);
        LinearLayoutManager lm1 = new LinearLayoutManager(this);
        lm1.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvMainTwo.setLayoutManager(lm1);


        List<String> sList = new ArrayList<>();
        sList.add("a");
        sList.add("a");
        sList.add("a");
        sList.add("a");
        mainJikanAdapter = new MainJikanAdapter(this);
        rvMainOne.setAdapter(mainJikanAdapter);
        rvMainTwo.setAdapter(mainJikanAdapter);
        mainJikanAdapter.addData(sList);
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initData() {

//        getPermission(new OnPermissionCallBack() {
//            @Override
//            public void permissionPass(String[] permissions) {
//                File file = new File("/storage/emulated/0/DCIM/Screenshots/Screenshot_2019-05-24-18-18-57-594_com.uphone.kingmall.png");
//                Map<String, RequestBody> mapRB = new HashMap<>();
//                mapRB.put("token", RequestBodyUtil.convertToRequestBody("d7f55d40ff168e4158b4833f37d51ae6"));
//                mapRB.put("uid", RequestBodyUtil.convertToRequestBody("8"));
//                MultipartBody.Part part = RequestBodyUtil.filesToMultipartBodyPart("file", file);
//                MyApp.apiService(ApiService.class)
//                        .userHightScoreUpload(mapRB, part)
//                        .compose(RxSchedulers.io_main())
//                        .doOnSubscribe(d -> showLoading())
//                        .doFinally(() -> closeLoading())
//                        .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from
//                                (MainActivity.this)))
//                        .subscribe(bean -> {
//                            LogUtils.e("bean:" + bean.string());
//                        }, throwable -> {
//                            // 异常
//                            LogUtils.e("throwable:" + throwable.toString());
//                            onFail();
//                        }, () -> {
//                            // 完成
//                        });
//            }
//
//            @Override
//            public void permissionRefuse(String[] permissions) {
//
//            }
//        }, Manifest.permission.WRITE_EXTERNAL_STORAGE);


    }

    @Override
    protected void initListener() {
        lvMain.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if ("other".equals(cAdapter.getData().get(i))) {
                    dlMain.openDrawer(Gravity.LEFT);
                } else {
                    IntentUtils.getInstance()
                            .with(MainActivity.this, HandbookContentActivity.class)
                            .start();
                }
            }
        });

        //语言选择
        lvLang.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                String language = null;
                switch (i) {
                    case 0:
                        //切换为简体中文
                        language = LanguageType.CHINESE.getLanguage();
                        tvMainLanguage.setText(getResources().getString(R.string.zhongwen));
                        break;
                    case 1:
                        //切换为英语
                        language = LanguageType.ENGLISH.getLanguage();
                        tvMainLanguage.setText(getResources().getString(R.string.yingyu));
                        break;
                    case 2:
                        //切换为日语
                        language = LanguageType.JAPAN.getLanguage();
                        tvMainLanguage.setText(getResources().getString(R.string.riwen));
                        break;
                    case 3:
                        //切换为韩语
                        language = LanguageType.KOREA.getLanguage();
                        tvMainLanguage.setText(getResources().getString(R.string.hanwen));
                        break;
                    default:
                        break;
                }
                changeLanguage(language);
            }
        });
    }


    /**
     * 如果是7.0以下，我们需要调用changeAppLanguage方法，
     * 如果是7.0及以上系统，直接把我们想要切换的语言类型保存在SharedPreferences中,然后重新启动MainActivity即可
     *
     * @param language
     */
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void changeLanguage(String language) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            LanguageUtils.changeAppLanguage(MainActivity.this, language);
        }
        SharedUtils.putShareLanguage(language, MainActivity.this);
        if ("en".equals(language)){
            MyApp.languageType = "2";
        }
        if ("ch".equals(language)){
            MyApp.languageType = "1";
        }
        if ("ja".equals(language)){
            MyApp.languageType = "3";
        }
        if ("ko".equals(language)){
            MyApp.languageType = "4";
        }
        Intent intent = new Intent(this, MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }

    @OnClick({R.id.ll_tomine, R.id.tv_to_read, R.id.tv_shouce, R.id.tv_jikan, R.id.ll_language, R.id.tv_mulu})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_tomine:
                IntentUtils.getInstance()
                        .with(this, MineActivity.class)
                        .start();
                break;
            case R.id.tv_to_read:
                IntentUtils.getInstance()
                        .with(this, HandbookContentActivity.class)
                        .start();
                break;
            case R.id.tv_shouce:
                llShouce.setVisibility(View.VISIBLE);
                llJikan.setVisibility(View.GONE);
                tvTitle.setText(getResources().getString(R.string.shouce));
                break;
            case R.id.tv_jikan:
                llShouce.setVisibility(View.GONE);
                llJikan.setVisibility(View.VISIBLE);
                tvTitle.setText(getResources().getString(R.string.jikan));
                break;
            case R.id.ll_language:
                if (langFlag == false) {
                    lvLang.setVisibility(View.VISIBLE);
                    langFlag = true;
                } else {
                    lvLang.setVisibility(View.GONE);
                    langFlag = false;
                }
                break;
            case R.id.tv_mulu:
                IntentUtils.getInstance()
                        .with(this, ContanctsActivity.class)
                        .start();
                break;
        }
    }

    @Override
    public void muluView(String responseBean, int status) {

    }

    @Override
    public void bannerView(String responseBean, int status) {

    }

    @Override
    public void publicationView(String responseBean, int status) {

    }

    @Override
    protected void initInject() {

    }
}
